<template>
  <view class="register" v-show="loading">
    <view class="content">
      <!-- 头部logo -->
      <view class="header">
        <image :src="logoImage"></image>
      </view>
      <!-- 主体 -->
      <view class="main">
        <wInput v-model="phoneData" type="number" maxlength="11" placeholder="手机号"></wInput>
        <wInput v-model="passData" type="password" maxlength="11" placeholder="登录密码" isShowPass></wInput>
        <wInput v-model="verCode" type="number" maxlength="6" setTime="120" placeholder="验证码" isShowCode ref="runCode" @setCode="getVerCode()"></wInput>
        <!-- #ifdef MP-WEIXIN -->
        <move-verify class="margin-top-sm round" @result="verifyResult" ref="verifyElement"></move-verify>
        <!-- #endif -->
      </view>

      <view class="flex flex-center" @click="startReg()">
        <wButton class="wbutton" text="注 册" :rotate="isRotate" bgColor="linear-gradient(to right, #0081ff, #4096f3)"></wButton>
      </view>
      <!-- 底部信息 -->
      <view class="footer">
        <text @tap="isShowAgree" class="cuIcon" style="font-size: 14px; margin-right: 0" :class="showAgree ? 'cuIcon-radiobox' : 'cuIcon-round'">请阅读并同意</text>
        <!-- 协议地址 -->
        <navigator url="../index/agree?id=18" open-type="navigate" class="text-cyan">《{{ app.title }}用户协议》</navigator>
        与
        <navigator url="../index/agree?id=19" open-type="navigate" class="text-cyan">《{{ app.title }}隐私协议》</navigator>
      </view>
    </view>
  </view>
</template>
<script>
let _this;
import moveVerify from '@/components/helang-moveVerify/helang-moveVerify.vue';
import wInput from '../../components/watch-login/watch-input.vue'; //input
import wButton from '../../components/watch-login/watch-button.vue'; //button
export default {
  components: {
    'move-verify': moveVerify,
    wInput,
    wButton
  },
  data() {
    return {
      //logo图片 base64
      logoImage: '',
      phoneData: '', // 用户/电话
      passData: '', //密码
      verCode: '', //验证码
      showAgree: false, //协议是否选择
      isRotate: false, //是否加载旋转
      resultOk: true,
      loading: false,
      barHeight: 0,
      app: this.app,
      referee: '' //组测携带的上级id
    };
  },
  onLoad(options) {
    // 扫二维码携带的参数
    if (options.assets) {
      this.referee = options.assets;
      this.app.assets = options.assets; //推广id赋值全局变量
    }
    // 点击小程序分享带的参数
    if (options.referee) {
      this.referee = options.referee;
      this.app.assets = options.referee; //推广id赋值全局变量
      console.log('接收到的 referee 参数是：', this.referee); // 输出: 接收到的 referee 参数是： 123
      // 接下来你可以根据 referee 参数进行页面初始化或其他逻辑处理
    }
    _this = this;
    uni.showLoading();
    this.geconfig();
    this.barHeight = uni.getSystemInfoSync().statusBarHeight;
  },
  methods: {
    geconfig() {
      uni.request({
        method: 'post',
        url: _this.app.apiHost + '/getlogo',
        header: { Authorization: '' },
        success: (data) => {
          console.log(data.data.code);
          if (data.data.code == 0) {
            let da = data.data.data;
            _this.logoImage = da.murl + da.logo;
            uni.hideLoading();
            _this.loading = true;
          }
        }
      });
    },
    verifyResult(res) {
      console.log(res);
      this.resultOk = res.flag;
    },
    /* 校验插件重置 */
    verifyReset() {
      this.$refs.verifyElement.reset();
      /* 删除当前页面的数据 */
      this.resultOk = false;
    },
    backone() {
      uni.navigateBack({
        delta: 1
      });
    },
    gohome() {
      uni.switchTab({
        url: '../index/index'
      });
    },
    isShowAgree() {
      //是否选择协议
      _this.showAgree = !_this.showAgree;
    },
    getVerCode() {
      //获取验证码
      if (_this.phoneData.length != 11) {
        uni.showToast({
          icon: 'none',
          position: 'bottom',
          title: '手机号不正确'
        });
        return false;
      }
      if (_this.resultOk != true) {
        uni.showToast({
          icon: 'none',
          position: 'bottom',
          title: '请拖动滑块验证'
        });
        return false;
      }
      uni.request({
        method: 'post',
        url: _this.app.apiHost + '/sendregmsg',
        data: { phone: _this.phoneData },
        header: { Authorization: '' },
        success: (data) => {
          _this.isfa = true;
          console.log(data);
          if (data.data.code == 0) {
            _this.$refs.runCode.$emit('runCode'); //触发倒计时（一般用于请求成功验证码后调用）
            _this.app.showMsg('发送成功');
          } else if (data.data.code == 2) {
            _this.$refs.runCode.$emit('runCode', parseInt(data.data.time));
            uni.showToast({
              title: data.data.msg,
              icon: 'error',
              position: 'bottom'
            });
          } else {
            _this.verifyReset();
            _this.$refs.runCode.$emit('runCode', 0);
            uni.showToast({
              title: data.data.msg,
              icon: 'error',
              position: 'bottom'
            });
          }
        },
        fail(a) {
          _this.$refs.runCode.$emit('runCode', 0);
          uni.showToast({
            title: '链接失败',
            icon: 'error',
            position: 'bottom'
          });
        }
      });
    },
    startReg() {
      //注册
      if (this.isRotate) {
        //判断是否加载中，避免重复点击请求
        return false;
      }
      if (this.showAgree == false) {
        uni.showToast({
          icon: 'none',
          position: 'bottom',
          title: '请阅读并同意用户与隐私协议'
        });
        return;
      }
      if (this.phoneData.length != 11) {
        uni.showToast({
          icon: 'none',
          position: 'bottom',
          title: '手机号格式错误'
        });
        return false;
      }
      if (this.passData.length < 6) {
        uni.showToast({
          icon: 'none',
          position: 'bottom',
          title: '密码长度最少6位'
        });
        return false;
      }
      if (this.verCode.length != 6) {
        uni.showToast({
          icon: 'none',
          position: 'bottom',
          title: '请输入6位验证码'
        });
        return false;
      }
      _this.isRotate = true;
      uni.request({
        url: _this.app.apiHost + '/register',
        method: 'POST',
        header: { Authorization: '' },
        data: { codeno: _this.verCode, phoneno: _this.phoneData, newpsw: _this.passData, referee: _this.referee },
        success: function (res) {
          if (res.data.code == 0) {
            _this.app.showMsg('注册成功!,请登陆', '../login/login');
          } else {
            _this.isRotate = false;
            uni.showToast({
              title: res.data.msg,
              icon: 'none',
              position: 'bottom'
            });
          }
        }
      });
    }
  }
};
</script>
<style>
@import url('../../components/watch-login/css/icon.css');
@import url('./css/main.css');
.dfan {
  position: absolute;
  top: 10px;
  left: 10px;
  border-radius: 20px;
  border: 1px solid #c1c1c1;
  padding: 3px 5px;
}
.main-list {
  margin: 20rpx 0;
}
.header {
  margin-top: 40rpx;
}
</style>
